using System;
using System.Collections;
using System.Globalization;
using System.Threading;
using System.Xml.Serialization;
namespace Genuilder.Extensions.EmbeddedResource
{
	internal static class NameGenerator
	{
		private static int uniqueIdCounter;
		private static Hashtable keywords;
		static NameGenerator()
		{
			NameGenerator.uniqueIdCounter = 0;
			NameGenerator.keywords = new Hashtable();
			string[] keywordlist = new string[]
			{
				"abstract",
				"event",
				"new",
				"struct",
				"as",
				"explicit",
				"null",
				"switch",
				"base",
				"extern",
				"object",
				"this",
				"bool",
				"false",
				"operator",
				"throw",
				"break",
				"finally",
				"out",
				"true",
				"byte",
				"fixed",
				"override",
				"try",
				"case",
				"float",
				"params",
				"typeof",
				"catch",
				"for",
				"private",
				"uint",
				"char",
				"foreach",
				"protected",
				"ulong",
				"checked",
				"goto",
				"public",
				"unchecked",
				"class",
				"if",
				"readonly",
				"unsafe",
				"const",
				"implicit",
				"ref",
				"ushort",
				"continue",
				"in",
				"return",
				"using",
				"decimal",
				"int",
				"sbyte",
				"virtual",
				"default",
				"interface",
				"sealed",
				"volatile",
				"delegate",
				"internal",
				"short",
				"void",
				"do",
				"is",
				"sizeof",
				"while",
				"double",
				"lock",
				"stackalloc",
				"else",
				"long",
				"static",
				"enum",
				"namespace",
				"string",
				"var"
			};
			string[] array = keywordlist;
			for (int j = 0; j < array.Length; j++)
			{
				string i = array[j];
				NameGenerator.keywords.Add(i.ToUpper(CultureInfo.InvariantCulture), i);
			}
		}
		public static int GetUniqueID()
		{
			Interlocked.Increment(ref NameGenerator.uniqueIdCounter);
			return NameGenerator.uniqueIdCounter;
		}
		
		public static string MakeValidCLRNamespace(string xsdNamespace, bool nameMangler2)
		{
			string result;
			if (xsdNamespace == null || xsdNamespace == string.Empty)
			{
				result = string.Empty;
			}
			else
			{
				xsdNamespace = xsdNamespace.Replace("http://", string.Empty);
				if (xsdNamespace == string.Empty)
				{
					result = string.Empty;
				}
				else
				{
					if (nameMangler2)
					{
						xsdNamespace = xsdNamespace.Replace('.', '_').Replace('-', '_');
					}
					string[] pieces = xsdNamespace.Split(new char[]
					{
						'/',
						'.',
						':',
						'-'
					});
					string clrNS = NameGenerator.MakeValidIdentifier(pieces[0], nameMangler2);
					for (int i = 1; i < pieces.Length; i++)
					{
						if (pieces[i] != string.Empty)
						{
							clrNS = clrNS + "." + NameGenerator.MakeValidIdentifier(pieces[i], nameMangler2);
						}
					}
					result = clrNS;
				}
			}
			return result;
		}
		public static string MakeValidIdentifier(string identifierName, bool nameMangler2)
		{
			if (nameMangler2)
			{
				if (char.IsDigit(identifierName[0]))
				{
					identifierName = "_" + identifierName;
				}
				identifierName = identifierName.Replace('.', '_').Replace('-', '_');
			}
			else
			{
				identifierName = CodeIdentifier.MakeValid(identifierName);
			}
			string result;
			if (NameGenerator.isKeyword(identifierName))
			{
				result = "@" + identifierName;
			}
			else
			{
				result = identifierName;
			}
			return result;
		}
		public static bool isKeyword(string identifier)
		{
			return NameGenerator.keywords.ContainsKey(identifier.ToUpper(CultureInfo.InvariantCulture));
		}
	}
}
